package com.tgy.algorithm.base._二分法;

public class _查找大于当前值的最左位置 {

    public static int getMaxLeftNum(int[] nums,int target) {

        int left = 0;
        int right = nums.length - 1;
        int mid;
        int location = -1;
        while (left <= right) {
            mid = left + ((right - left) >> 1);
            if (nums[mid] > target) {
                location = mid;
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }

        return location;
    }

    public static int getMaxLeftEqualNum(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;
        int mid;
        int location = -1;
        while (left <= right) {
            mid = left + ((right - left) >> 1);
            if (nums[mid] > target) {
                location = mid;
                right = mid - 1;
            }else if(nums[mid] == target) {
                location = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return location;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,3,4,5};
        int index = getMaxLeftEqualNum(nums, 3);
        System.out.println(index);
    }
}
